home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 October: Mac OS SDK / Dev.CD Oct 97 SDK1.toast / Development Kits (Disc 1) / Apple Color OneScanner SDK / Scan Image 1.0 / Source / scrolls.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-08-24  |  6.9 KB  |  322 lines  |  [TEXT/CWIE]

  1. /*************************************************************************************
  2. #
  3. #        scrolls.c
  4. #
  5. #        This segment handles the scroll bars.
  6. #
  7. #        Author(s):     Michael Marinkovich
  8. #                    Apple Developer Technical Support
  9. #                    marink@apple.com
  10. #
  11. #        Modification History: 
  12. #
  13. #            4/3/96        MWM     Initial coding                     
  14. #
  15. #        Copyright © 1992-96 Apple Computer, Inc., All Rights Reserved
  16. #
  17. #
  18. #        You may incorporate this sample code into your applications without
  19. #        restriction, though the sample code has been provided "AS IS" and the
  20. #        responsibility for its operation is 100% yours.  However, what you are
  21. #        not permitted to do is to redistribute the source as "DSC Sample Code"
  22. #        after having made changes. If you're going to re-distribute the source,
  23. #        we require that you make it clear in the source that the code was
  24. #        descended from Apple Sample Code, but that you've made changes.
  25. #
  26. *************************************************************************************/
  27.  
  28. #include <Events.h>
  29. #include <ToolUtils.h>
  30. #include <Gestalt.h>
  31. #include <OSUtils.h>
  32.  
  33.  
  34. #include "App.h"
  35. #include "Proto.h"
  36.  
  37. //----------------------------------------------------------------------
  38. //    Globals
  39. //----------------------------------------------------------------------
  40.  
  41. extern Boolean        gInBackground;
  42.  
  43.  
  44. //----------------------------------------------------------------------
  45. //
  46. //    InstallScrollBars -
  47. //                 
  48. //
  49. //----------------------------------------------------------------------
  50.  
  51. void InstallScrollBars(WindowRef window, DocHnd doc)
  52. {
  53.     Rect            bounds;
  54.     ControlRef        hControl;
  55.     ControlRef        vControl;
  56.     
  57.     
  58.     if (doc != nil)
  59.     {
  60.         // calc horiz scroll bar
  61.         bounds = window->portRect;
  62.         
  63.         bounds.right++;
  64.         bounds.left = bounds.right - (kScrollWidth + 1);
  65.         bounds.top--;
  66.         bounds.bottom -= (kScrollWidth - 1);
  67.         
  68.         hControl = NewControl(window, &bounds, (ConstStr255Param)"\p", true,
  69.                               0, 0, 0, scrollBarProc, nil);
  70.  
  71.         if (hControl != nil)
  72.             (**doc).hScroll = hControl;
  73.                                       
  74.         // calc vert scroll bar
  75.         bounds = window->portRect;
  76.     
  77.         bounds.left--;
  78.         bounds.right -= (kScrollWidth - 1);
  79.         bounds.top = bounds.bottom - kScrollWidth;
  80.         bounds.bottom++;
  81.         
  82.     
  83.         vControl = NewControl(window, &bounds, (ConstStr255Param)"\p", true,
  84.                               0, 0, 0, scrollBarProc, nil);
  85.     
  86.         if (vControl != nil)
  87.             (**doc).vScroll = vControl;
  88.     }
  89.     
  90. }
  91.  
  92.  
  93. //----------------------------------------------------------------------
  94. //
  95. //    AdjustScrollValues -
  96. //                 
  97. //
  98. //----------------------------------------------------------------------
  99.  
  100. void AdjustScrollValues(WindowRef window)
  101. {
  102.     Rect                    contRect;
  103.     Rect                    picFrame;
  104.     ControlHandle            hCntrl;
  105.     ControlHandle            vCntrl;
  106.     short                    oldValue;
  107.     short                    lines;
  108.     short                    max;
  109.     DocHnd                    doc;
  110.     
  111.     doc = (DocHnd)GetWRefCon(window);
  112.     
  113.     if (doc != nil) {
  114.         hCntrl = (**doc).hScroll;
  115.         vCntrl = (**doc).vScroll;
  116.  
  117.         GetContRect(window,&contRect);
  118.         
  119.         if ((**doc).pict != nil || (**doc).world != nil)
  120.             picFrame = (**doc).world->portRect;
  121.         else
  122.             SetRect(&picFrame, 0, 0, 0, 0);
  123.             
  124.         oldValue = GetCtlValue(vCntrl);
  125.         lines = picFrame.bottom - picFrame.top;
  126.         max = lines - contRect.bottom ;
  127.         if ( max < 0 ) max = 0;
  128.         SetCtlMax(vCntrl, max);
  129.     
  130.         if(oldValue + contRect.bottom > lines)
  131.             SetCtlValue(vCntrl, max);
  132.         
  133.         oldValue = GetCtlValue(hCntrl);
  134.         lines = picFrame.right - picFrame.left;
  135.         max = lines - contRect.right;
  136.         if ( max < 0 ) max = 0;
  137.         SetCtlMax(hCntrl, max);
  138.     
  139.         if(oldValue + contRect.right > lines)
  140.             SetCtlValue(hCntrl, max);
  141.                 
  142.     }        
  143.             
  144. }
  145.  
  146.  
  147. //----------------------------------------------------------------------
  148. //
  149. //    AdjustScrollbars -
  150. //                 
  151. //
  152. //----------------------------------------------------------------------
  153.  
  154. void AdjustScrollbars(WindowRef window, Boolean resize)
  155. {
  156.     ControlRef        hCtl;
  157.     ControlRef        vCtl;
  158.     Rect            r;
  159.     DocHnd             doc;
  160.  
  161.     
  162.     doc = (DocHnd)GetWRefCon(window);
  163.     
  164.     if (doc != nil && resize) {
  165.         hCtl = (**doc).hScroll;
  166.         vCtl = (**doc).vScroll;
  167.         GetContRect(window, &r);
  168.         
  169.         // resize horizontal
  170.         MoveControl(hCtl, -1, r.bottom);
  171.         SizeControl(hCtl, (r.right - r.left) + 2, kScrollWidth + 1);
  172.         
  173.         // resize vertical
  174.         MoveControl(vCtl, r.right, -1);
  175.         SizeControl(vCtl, kScrollWidth + 1, r.bottom + 2);
  176.     }
  177.     
  178.     // adjust scrollbar values to match
  179.     AdjustScrollValues(window);
  180. }
  181.  
  182.  
  183. //----------------------------------------------------------------------
  184. //
  185. //    ScrollActionProc -
  186. //                 
  187. //
  188. //----------------------------------------------------------------------
  189.  
  190. pascal void ScrollActionProc(ControlRef control, short part)
  191. {
  192.     WindowRef        window;
  193.     short            amount;
  194.     short            value;
  195.     short            max;
  196.     short            hAmount;
  197.     short            vAmount;
  198.     DocHnd            doc;
  199.     
  200.     window = (**control).contrlOwner;
  201.     doc = (DocHnd)GetWRefCon(window);
  202.     
  203.     if (doc != nil) {
  204.         if ( part != 0 ) {                
  205.             window = (*control)->contrlOwner;
  206.             hAmount = vAmount = 0;
  207.             
  208.             value = GetCtlValue(control);    
  209.             max = GetCtlMax(control);        
  210.         
  211.             switch ( part ) {
  212.                 case inUpButton:
  213.                 case inDownButton:        // one line
  214.                         amount = 8;
  215.                         break;
  216.                         
  217.                 case inPageUp:            // one page
  218.                 case inPageDown:
  219.                         amount = 64;
  220.                         break;
  221.             }
  222.             
  223.             if ( (part == inDownButton) || (part == inPageDown) )
  224.                 amount = -amount;        // reverse direction for a downer
  225.             
  226.             amount = value - amount;
  227.             if ( amount < 0 )
  228.                 amount = 0;
  229.             else if ( amount > max )
  230.                 amount = max; 
  231.             SetCtlValue(control, amount);
  232.             
  233.             amount = value - amount;        // calculate the real change
  234.             
  235.             if ( amount != 0 ) {
  236.                 if (control == (**doc).hScroll)
  237.                     hAmount = amount;
  238.                 else
  239.                     vAmount = amount ;
  240.                         
  241.                 MyScrollPicture(window,hAmount, vAmount);
  242.             }
  243.         }    
  244.     }    
  245. }
  246.     
  247.  
  248. //----------------------------------------------------------------------
  249. //
  250. //    MyScrollPicture -
  251. //                 
  252. //
  253. //----------------------------------------------------------------------
  254.  
  255. void MyScrollPicture(WindowRef window, short hs, short vs)
  256. {
  257.     GWorldPtr        oldWorld;
  258.     GWorldPtr        theWorld;
  259.     GDHandle        oldGD;
  260.     PixMapHandle    thePix;
  261.     Rect            contRect;
  262.     Rect            pictRect;
  263.     DocHnd            doc;
  264.     
  265.     doc = (DocHnd)GetWRefCon(window);
  266.     
  267.     if (doc != nil) 
  268.     {
  269.         GetGWorld(&oldWorld, &oldGD);
  270.         SetPort(window);
  271.         
  272.         GetContRect(window, &contRect);
  273.         
  274.         theWorld = (**doc).world;
  275.         pictRect = theWorld->portRect;
  276.         
  277.         thePix = GetGWorldPixMap(theWorld);
  278.         
  279.         if (LockPixels(thePix)) 
  280.         {
  281.             ClipRect(&contRect);
  282.             
  283.             SetGWorld((CGrafPtr)window, nil);
  284.             
  285.             OffsetRect(&pictRect, -GetCtlValue((**doc).hScroll), 
  286.                       -GetCtlValue((**doc).vScroll));
  287.  
  288.             CopyBits((BitMap *) *thePix, &window->portBits,
  289.                     &theWorld->portRect, &pictRect, srcCopy, nil);
  290.             
  291.             UnlockPixels(thePix);
  292.             
  293.             SetGWorld(oldWorld, oldGD);
  294.         }
  295.             
  296.         ClipRect(&window->portRect);
  297.  
  298.     }
  299.         
  300. }    
  301.  
  302.  
  303. //----------------------------------------------------------------------
  304. //
  305. //    GetContRect -
  306. //                 
  307. //
  308. //----------------------------------------------------------------------
  309.  
  310. void GetContRect(WindowRef window, Rect *contRect)
  311. {
  312.     if (contRect != nil)
  313.     {
  314.         *contRect = window->portRect;    // get the content rect for our window
  315.         
  316.         contRect->right -= 15;            // subtract the scroll bars from the rect
  317.         contRect->bottom -= 15;
  318.     }    
  319.  
  320.  
  321. }
  322.